/**
 * 
 */
package de.gee.erep.server.services;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;

import javax.xml.stream.XMLStreamException;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import de.gee.erep.client.calls.rpc.Initialization;
import de.gee.erep.server.xml.CountryParser;
import de.gee.erep.server.xml.ErepParser;
import de.gee.erep.shared.entities.Country;

/**
 * @author Matthew Gee created: 13.03.2011
 */
public class InitService extends RemoteServiceServlet implements Initialization {

	/***/
	private static final long serialVersionUID = 1L;

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.gee.erep.client.calls.rpc.Initialization#getCountries()
	 */
	@Override
	public final ArrayList<Country> getCountries() {
		return getCountries(0, -1);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.gee.erep.client.calls.rpc.Initialization#getCountries(int, int)
	 */
	@Override
	public final ArrayList<Country> getCountries(final int start,
			final int count) {
		ErepParser<Country> parser = new CountryParser(
				"./Ressources/temp/countries.xml");
		try {
			parser.parse();
		} catch (XMLStreamException e) {

			e.printStackTrace();
		}
		return new ArrayList<Country>(parser.getResult()
				.subList(
						start,
						count == -1 ? parser.getResult().size() - 1 : start
								+ count - 1));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.gee.erep.client.calls.rpc.Initialization#getUpdatedCountries()
	 */
	@Override
	public final ArrayList<Country> getUpdatedCountries() {
		return getUpdatedCountries(0, -1);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.gee.erep.client.calls.rpc.Initialization#getUpdatedCountries(int,
	 * int)
	 */
	@Override
	public final ArrayList<Country> getUpdatedCountries(final int start,
			final int count) {
		ErepParser<Country> parser = new CountryParser(
				"war/Ressources/temp/countries.xml");
		try {
			parser.parseUpdate();
		} catch (XMLStreamException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return new ArrayList<Country>(parser.getResult()
				.subList(
						start,
						count == -1 ? parser.getResult().size() - 1 : start
								+ count - 1));
	}

}
